package com.gk.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 远程词典CRUD的Controller
 */
@Api(tags = "检索系统后台--远程词典模块")
@RestController
@RequestMapping("/dic")
public class PoemDicController {


    /**
     * 获取远程词典列表
     * @param request
     * @return
     */
    @ApiOperation(value = "获取远程词典列表")
    @ApiImplicitParam
    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public List<String> findAllDics(HttpServletRequest request) {

        //获取当前项目的绝对路径
        String path = request.getServletContext().getRealPath("/");

        BufferedReader br = null;
        //读取文件
        ArrayList<String> list = null;
        try {
            File file = new File(path, "init.dic");
            br = new BufferedReader(new FileReader(file));

            list = new ArrayList<>();
            String keywords = null;
            while ((keywords = br.readLine()) != null){
                list.add(keywords);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            if (br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return  list;
    }

    @ApiOperation(value = "添加到远程词典")
    @ApiImplicitParam
    @RequestMapping(value = "/save", method = RequestMethod.GET)
    public Map<String, Object> saveDic(String keyword, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();

        //对获取的数据进行去空格处理
        keyword = StringUtils.trimAllWhitespace(keyword);

        //获取项目路径
        String path = request.getServletContext().getRealPath("/");

        BufferedReader br = null;
        BufferedWriter bw = null;

        try {
            File file = new File(path, "init.dic");
            br = new BufferedReader(new FileReader(file));
            bw = new BufferedWriter(new FileWriter(file, true));

            while (true) {
                String line = br.readLine();
                if (line == null) {
                    bw.write(keyword);
                    result.put("success",true);
                    break;
                } else {
                    if (line.equals(keyword)) {
                        throw new RuntimeException("关键词已经存在,无须重复添加!!!");
                    }
                }
            }
        } catch (IOException | RuntimeException  e) {
            result.put("success",false);
            result.put("message",e.getMessage());
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    @ApiOperation(value = "删除远程词典中的热词")
    @ApiImplicitParam(name = "keyword", value = "远程词典热词", dataType = "String")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public Map<String, Object> deleteKeyword (@RequestParam("keyword") String keyword, HttpServletRequest request) {

        Map<String, Object> result = new HashMap<>();

        //获取项目绝对路径
        String path = request.getServletContext().getRealPath("/");
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            File file = new File(path, "init.dic");
            //使用BufferedReader读取文件
            br = new BufferedReader(new FileReader(file));

            StringBuilder sb = new StringBuilder();
            while (true) {
                String line = br.readLine();
                if (line == null) break;
                if (!line.equals(keyword)) {
                    sb.append(line).append("\r\n"); //回车换行
                }
            }

            //将sb写入指定文件
            bw = new BufferedWriter(new FileWriter(file));
            bw.write(sb.toString());
            result.put("success",true);
        } catch (IOException e) {
            e.printStackTrace();
            result.put("success",false);
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }


}

